package com.jiantong.web.controller.news;

import com.jiantong.news.model.Thinktank;
import com.jiantong.news.service.ThinktankReadService;
import com.jiantong.news.service.ThinktankWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Created by mz on 9/5/17.
 *
 */
@RestController
@Slf4j
public class ThinktankController {

    @RpcConsumer
    private ThinktankReadService thinktankReadService;

    @RpcConsumer
    private ThinktankWriteService thinktankWriteService;

    @PostMapping(value = "/api/create/think/tank")
    public Long createThinkTank(@RequestBody Thinktank thinktank){
        if(thinktank.getId() != null){
            log.error("this thinktank is exist");
            throw new OPClientException("id is exist");
        }
        Response<Long> response = thinktankWriteService.create(thinktank);
        if(!response.isSuccess()){
            log.error("failed to create thinktank case = {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @PutMapping(value = "/api/update/think/tank")
    public Boolean updateThinkTank(@RequestBody Thinktank thinktank){
        if(thinktank.getId() == null){
            log.error("this id is not exist");
            throw new OPClientException("id is not exist");
        }
        Response<Boolean> response = thinktankWriteService.update(thinktank);
        if(!response.isSuccess()){
            log.error("failed to update think tank case = {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @GetMapping(value = "/api/paging/think/tank/by/type")
    public Paging<Thinktank> thinkTankPaging(@RequestParam Integer pageNo,
                                             @RequestParam Integer pageSize,
                                             @RequestParam Integer type){

        Response<Paging<Thinktank>> response = thinktankReadService.pagingWeb(pageNo,pageSize,type);
        if(!response.isSuccess()){
            log.error("failed to paging think tank type = {} ,case = {}",type,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @GetMapping(value = "/api/paging/think/tank/by/key/word")
    public Paging<Thinktank> thinkTankSearchPaging(@RequestParam Integer pageNo,
                                                   @RequestParam Integer pageSize,
                                                   @RequestParam String keyWord,
                                                   @RequestParam Integer type){
        Response<Paging<Thinktank>> response = thinktankReadService.pagingThinktankKeyword(pageNo,pageSize,keyWord,type);
        if(!response.isSuccess()){
            log.error("failed to paging think tank keyWord = {} ,case = {}",keyWord,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    @GetMapping(value = "/api/find/think/tank/by/id")
    public Thinktank findThinkTankById(@RequestParam Long id){
        if(id == null){
            log.error("id is null");
            throw new OPClientException("id is null");
        }
        Response<Thinktank> response = thinktankReadService.finById(id);
        if(!response.isSuccess()){
            log.error("failed to find think tank id = {} ,case = {}",id,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}
